package framework.office.apache.poi.xlsx;
import java.io.File;
import java.io.IOException;

import org.apache.poi.POITextExtractor;
import org.apache.poi.extractor.ExtractorFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xmlbeans.XmlException;
public class ReadExcel {
	/**
	 * 读取xlsx文本
	 * 
	 * @param filePath
	 */
	public void loadXlsxText(String filePath) {
		File inputFile = new File("D://test.xlsx");
		try {
			POITextExtractor extractor = ExtractorFactory.createExtractor(inputFile);
			System.out.println(extractor.getText());
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (OpenXML4JException e) {
			e.printStackTrace();
		} catch (XmlException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 读取office 2007 xlsx
	 * 
	 * @param filePath
	 */
	public void loadXlsx(String filePath) {
		// 构造 XSSFWorkbook 对象，strPath 传入文件路径
		XSSFWorkbook xwb = null;
		try {
			xwb = new XSSFWorkbook("D://test.xlsx");
		} catch (IOException e) {
			System.out.println("读取文件出错");
			e.printStackTrace();
		}
		// 读取第一章表格内容
		XSSFSheet sheet = xwb.getSheetAt(0);
		xwb.getSheetAt(1);
		// 定义 row、cell
		XSSFRow row;
		String cell;
		// 循环输出表格中的内容
		for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
			row = sheet.getRow(i);
			for (int j = row.getFirstCellNum(); j < row.getPhysicalNumberOfCells(); j++) {
				// 通过 row.getCell(j).toString() 获取单元格内容，
				cell = row.getCell(j).toString();
				System.out.print(cell + "/t");
			}
			System.out.println("");
		}
	}
	public static void main(String[] args) {
		ReadExcel readExcel = new ReadExcel();
		readExcel.loadXlsx("");
	}
}
